package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Referer;

public class GenericRefererDAO extends AutoKeys implements
		org.jetnuke.dao.RefererDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericRefererDAO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.RefererModel#selectAll()
	 */
	public List selectAll() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("RefererModel.selectAll"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.RefererModel#deleteAll()
	 */
	public void deleteAll() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("deleteAll() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("RefererModel.deleteAll"));
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("deleteAll() - end"); //$NON-NLS-1$
		}
	}

	protected List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			Referer u = new Referer();
			fillRefererFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/**
	 * @param u
	 * @param rs
	 */
	private void fillRefererFromResultSet(Referer u, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillRefererFromResultSet(Referer, ResultSet) - start"); //$NON-NLS-1$
		}

		u.setRID(rs.getInt("RID"));
		u.setURL(rs.getString("URL"));

		if (logger.isDebugEnabled()) {
			logger.debug("fillRefererFromResultSet(Referer, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.RefererModel#countAll()
	 */
	public int countAll() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("countAll() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(SystemGlobals.getSql("RefererModel.countAll"));

		ResultSet rs = p.executeQuery();
		rs.next();

		int r = rs.getInt(1);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("countAll() - end"); //$NON-NLS-1$
		}
		return r;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.RefererModel#selectOrderRID(int)
	 */
	public List selectOrderRID(int limit) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectOrderRID(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("RefererModel.selectOrderRID"));
		p.setInt(1, limit);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectOrderRID(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.RefererModel#addNew(org.jetnuke.entities.Referer)
	 */
	public int addNew(Referer ref) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Referer) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("RefererModel.addNew");

		initNewReferer(ref, p);

		int id = this.executeAutoKeysQuery(p);

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Referer) - end"); //$NON-NLS-1$
		}
		return id;
	}

	/**
	 * @param ref
	 * @param p
	 */
	protected void initNewReferer(Referer ref, PreparedStatement p)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("initNewReferer(Referer, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setString(1, ref.getURL());

		if (logger.isDebugEnabled()) {
			logger.debug("initNewReferer(Referer, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

}
